import dask.dataframe as dd
import dask_ml.preprocessing as dpp
import dask_ml.decomposition as dde
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# 读取csv文件
df = dd.read_csv('WCPFC_PS_M_Grid1_Temp_SSH_MLT_2010_2019.csv',assume_missing=True)

# 选择需要的数据段
cols = ['Year', 'Month', 'days', 'skj_c_una', 'skj_c_log', 'skj_c_dfad', 'skj_c_afad', 'Temp_0', 'Temp_50', 'Temp_100', 'Temp_150', 'Temp_200', 'Temp_250', 'Temp_300', 'SSH', 'MLT']
df = df[cols]

# 将时间数据转化为数值型数据
df['Year'] = df['Year'] - df['Year'].min()
df['Month'] = df['Month'] - 1

# 数据清洗
df = df.dropna()  # 去除缺失值
df = df[(df['skj_c_una'] > 0) & (df['skj_c_log'] > 0) & (df['skj_c_dfad'] > 0) & (df['skj_c_afad'] > 0)]  # 去除异常值

# 将捕捞数据进行标准化
scaler = dpp.StandardScaler()
#env_data = scaler.fit_transform(df[['Temp_0', 'Temp_50', 'Temp_100', 'Temp_150', 'Temp_200', 'Temp_250', 'Temp_300', 'SSH', 'MLT']])

env_data = df[['skj_c_una', 'skj_c_log', 'skj_c_dfad', 'skj_c_afad']].to_dask_array(lengths=True)



# 进行PCA降维
#pca = dde.PCA(n_components=3)
#pca_data = pca.fit_transform(env_data)

pca = dde.PCA(n_components=3)
pca_data = pca.fit_transform(env_data.compute_chunk_sizes())

# 将PCA降维后的结果与SKJ围网产量进行关联分析
skj_data = df[['Temp_0', 'Temp_50', 'Temp_100', 'Temp_150', 'Temp_200', 'Temp_250', 'Temp_300', 'SSH', 'MLT']]
skj_data = skj_data.compute()  # 将dask dataframe转化为pandas dataframe
result = pd.concat([pd.DataFrame(pca_data, columns=['PC1', 'PC2', 'PC3']), skj_data], axis=1)

# 绘制关联分析图
import seaborn as sns
sns.pairplot(result, x_vars=['PC1', 'PC2', 'PC3'], y_vars=['Temp_0', 'Temp_50', 'Temp_100', 'Temp_150', 'Temp_200', 'Temp_250', 'Temp_300', 'SSH', 'MLT'])
plt.savefig('test11.png')
